<?php
// +----------------------------------------------------------------------
// | SL 2018.8.17
// +----------------------------------------------------------------------
// | Copyright (c) 2018~2018 http://sl.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zq <1040657944@qq.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 生成操作按钮
 * @param array $operate 操作按钮数组
 */
function showOperate($operate = [])
{
    if(empty($operate)){
        return '';
    }
    $option = '';
    foreach($operate as $key=>$vo){
//        if(authCheck($vo['auth'])){
            $option .= ' <a href="' . $vo['href'] . '"><button type="button" class="layui-btn  layui-btn-' . $vo['btnStyle'] . ' layui-btn-sm">'.
                '<i class="' . $vo['icon'] . '"></i> ' . $key . '</button></a>';
//        }
    }

    return $option;
}

/**
 * 显示所有的菜单权限（目录形式）
 * @param $data
 * @param int $pid
 * @param int $level
 * @param int $index
 * @return array
 */
function rule_to_tree($data, $pid = 0,$level=0,$index=0)
{
    $arr = array();
//    dd(11,$data);
    foreach ($data as $k => $v) {
        if ($pid == $v['pid']) {
            $v['href_name'] = $v['controller_name']."/".$v['action_name'];
            $v['name'] = $v['node_name'];
            $v['level'] = $level + 1;
            $v['spread'] = false;

            if($index!=0){
                if($index!=$v['level']){
                    $v['children'] = rule_to_tree($data, $v['id'], $v['level'],$index);
                    if (empty($v['children'])) {
//                        unset($v['children']);
                        $v['children'] = [];
                    }
                }
            }else{
                $v['children'] = rule_to_tree($data, $v['id'], $v['level'],$index);
                if (empty($v['children'])) {
//                    unset($v['children']);
                    $v['children'] = [];
                }
            }
            array_push($arr, $v);
        }
    }
    return $arr;
}

/**
 * 显示所有的菜单权限（列表形式）
 */
function rule_to_list($nav,$pid = 0,$level = 0){
    static $data = [];
    foreach($nav as $key=>$value){
        if($value['pid'] == $pid){
            $value['href_name'] = $value['controller_name']."/".$value['action_name'];
            $value['name'] = $value['node_name'];
            $value['level'] = $level+1;
            $value['spread'] = false;

            $level_mark = '';
            for($i=1;$i<$value['level'];$i++){
                $level_mark .= '&nbsp;&nbsp;├&nbsp;&nbsp;';
            }
            $value['level_mark'] = $level_mark;
            if($level > 0){
                if(!array_key_exists('children',$data[$value['pid']])){
                    $data[$value['pid']]['children'] = array();
                }
                if(!in_array($value['id'],$data[$value['pid']]['children'])){
                    array_push($data[$value['pid']]['children'],$value['id']);
                }
            }
            $data[$value['id']] = $value;
            rule_to_list($nav,$value['id'],$value['level']);
        }
    }
    return $data;
}


/**
 * 转成数组
*/
function selectArray($param)
{
   return  json_decode(json_encode($param),true);
}